package com.example.test.controller;

import com.example.test.common.ResultBody;
import com.example.test.query.ActivitiGroupQuery;
import com.example.test.query.ActivitiLinkUserQuery;
import com.example.test.query.ActivitiUnLinkUserQuery;
import com.github.pagehelper.PageInfo;
import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：dengjue
 * @date ：Created in 2021/7/29 17:56
 * @description ： activiti 用户组
 */
@Controller
@RequestMapping("/activiti/group")
public class ActivitiGroupController {

    @Resource
    private IdentityService identityService;


    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView("/activitiGroup");
        return mv;
    }

    @PostMapping("/save")
    @ResponseBody
    public ResultBody save(GroupEntity groupEntity){
        if (StringUtils.isEmpty(groupEntity.getId())){
            groupEntity.setId(null);
            identityService.saveGroup(groupEntity);
        }{
            Group group = identityService.createGroupQuery().groupId(groupEntity.getId()).singleResult();
            group.setName(groupEntity.getName());
            group.setType(groupEntity.getType());
            identityService.saveGroup(group);
        }
        return ResultBody.success("用户组保存成功!");
    }

    @DeleteMapping("/delete")
    @ResponseBody
    public ResultBody delete(@RequestParam("id") String id){
        identityService.deleteGroup(id);
        return ResultBody.success("用户组删除成功!");
    }

    @GetMapping("/list")
    @ResponseBody
    public ResultBody<PageInfo<Group>> list(ActivitiGroupQuery groupQuery){
        Integer offset = (groupQuery.getPageNum() - 1) * groupQuery.getPageSize();
        List<Group> groupList = identityService.createGroupQuery().orderByGroupId().asc()
                .listPage(offset,groupQuery.getPageSize());
        PageInfo<Group> pageInfo = new PageInfo(groupList);
        long total = identityService.createGroupQuery().count();
        pageInfo.setTotal(total);
        return ResultBody.success(pageInfo);
    }

    @GetMapping("/userlist")
    @ResponseBody
    public ResultBody<List<User>> userlist(@RequestParam("groupId") String groupId){
        List<User> userList = identityService.createUserQuery().memberOfGroup(groupId).list();
        return ResultBody.success(userList);
    }


    @GetMapping("/addUserPage")
    public ModelAndView addUserPage(@RequestParam("groupId") String groupId){
        ModelAndView mv = new ModelAndView("/activitiAddUser");
        mv.addObject("groupId",groupId);
        return mv;
    }


    @PostMapping("/linkUser")
    @ResponseBody
    public ResultBody linkUser(@RequestBody ActivitiLinkUserQuery linkUserQuery){
        List<String> userIds = linkUserQuery.getUserIds();
        if (userIds != null && userIds.size()>0){
            userIds.forEach(userId ->{
                identityService.createMembership(userId,linkUserQuery.getGroupId());
            });
        }
        return ResultBody.success("关联成功");
    }

    @GetMapping("/delLinkUser")
    @ResponseBody
    public ResultBody delUser(@RequestParam("groupId") String groupId,
                                          @RequestParam("userId") String userId){
        identityService.deleteMembership(userId,groupId);
        return ResultBody.success("取消关联成功");
    }



    @GetMapping("/unLinkUserlist")
    @ResponseBody
    public ResultBody<List<User>> unLinkUserlist(ActivitiUnLinkUserQuery unLinkUserQuery){
        List<User> groupUserList = identityService.createUserQuery().memberOfGroup(unLinkUserQuery.getGroupId()).list();

        List<User> userList = identityService.createUserQuery().orderByUserId().asc().list();
        userList.removeAll(groupUserList);
        return ResultBody.success(userList);
    }
}
